/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-maintain
*
* @File name : MaintainController.java
*
* @Author : Wang HuiCheng
*
* @Date : 2016年10月31日
*
----------------------------------------------------------------------------------
*     Date              Who              Version     Comments
* 1.  2016年10月31日    Wang HuiCheng    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.maintenanceorder.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.maintenanceorder.MaintenanceDto;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.marketing.common.Response;
import com.yonyou.gmmc.service.maintenanceorder.constant.MaintenanceorderConstant;
import com.yonyou.gmmc.service.maintenanceorder.entity.EvaluationResultPO;
import com.yonyou.gmmc.service.maintenanceorder.service.MaintenanceorderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 维修履历controller
 * 
 * @author
 * @date 2017年3月7日
 */
@RestController
@RequestMapping(value = MaintenanceorderConstant.BASE_PATH + "/" + MaintenanceorderConstant.VERSION)
@Api("维修履历")
public class MaintenanceordereController {
	private Logger logger = Logger.getLogger(MaintenanceordereController.class);
	@Autowired
	private MaintenanceorderService maintenanceorderService;

	/**
	 * 维修履历查询
	 * 
	 * @author QianKun
	 * @date 2017年3月7日
	 * @param vin
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/maintenanceorder", method = RequestMethod.GET)
	@ApiOperation(value = "维修履历查询", notes = "维修履历查询")
	public @ResponseBody Map<String,Object> getMaintenance(
			@RequestParam(value = "userId_", required = false) String userId_
			,@RequestParam(value = "vin", required = false) String vin
			,HttpServletRequest request) {
	    logger.info("--维修履历查询--");
		String userId="";
		if(!CheckUtil.NullOrEmpty(userId_)){
			userId = userId_;
    	}else{
    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
    	}
	    Map<String,Object> map = maintenanceorderService.getMaintenance(userId,vin);
		return map;
	}

	/**
	 * 维修履历详情查询
	 * 
	 * @author 
	 * @date 2017年3月7日
	 * @param vin
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "维修履历详情查询", notes = "维修履历详情查询")
	public List<MaintenanceDto> getMaintenanceById(
			@RequestParam("maintenanceWorkOrderNum") String maintenanceWorkOrderNum
			,@RequestParam("dealerCode") String dealerCode) {
		List<MaintenanceDto> dtoList = maintenanceorderService.getMaintenanceById(maintenanceWorkOrderNum,dealerCode);
		return dtoList;
	}
	/**
     * 查看工单评价
     * 
     * @param info
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addEvaluation", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查看工单评价", notes = "查看工单评价")
    public Response<EvaluationResultPO> addEvaluation(
    		@RequestParam(required = true, defaultValue = "") Integer type,
    		@RequestParam(required = true, defaultValue = "") String orderNumber,
    		@RequestParam(required = true, defaultValue = "") Integer evaType, //0未评价   1已评价
    		@RequestParam(value = "userId_", required = false) String userId_,
    		HttpServletRequest request) {
        try {
        	logger.info("MaintenanceordereController.addEvaluation==>end...");
        	String userId="";
    		if(!CheckUtil.NullOrEmpty(userId_)){
    			userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            logger.info("查看工单评价开始userId："+userId);
            EvaluationResultPO result = maintenanceorderService.addEvaluation(type,orderNumber,userId,evaType);
            logger.info("查看工单评价结束");
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("Exception:", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("MaintenanceordereController.addEvaluation==>end...");
        }
    }
    /**
     * 提交工单评价
     * 
     * @param info
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveEvaluation", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "提交工单评价", notes = "提交工单评价")
    @ResponseBody
    public Response<Map<String, Object>> saveEvaluation(@RequestBody EvaluationResultPO evaluationResultPO,
    		@RequestParam(value = "userId_", required = false) String userId_,
    		HttpServletRequest request) {
        try {
        	logger.info("MaintenanceordereController.saveEvaluation==>start...");
        	String userId="";
    		if(!CheckUtil.NullOrEmpty(userId_)){
    			userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            logger.info("提交工单评价开始openId："+userId);
            Map<String, Object> result = maintenanceorderService.saveEvaluation(evaluationResultPO,userId);
            logger.info("提交工单评价结束");
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("Exception:", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("MaintenanceordereController.saveEvaluation==>end...");
        }
    }
    /**
     * 校验是否存在可评价工单
     * 
     * @param info
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/checkEvaluation", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "校验是否存在可评价工单", notes = "校验是否存在可评价工单")
    public Response<Map<String, Object>> checkEvaluation(
    		@RequestParam(required = true, defaultValue = "") Integer type,
    		@RequestParam(required = true, defaultValue = "") String orderNumber,
    		@RequestParam(value = "userId_", required = false) String userId_,
    		HttpServletRequest request) {
        try {
        	logger.info("MaintenanceordereController.checkEvaluation==>start...");
        	String userId="";
    		if(!CheckUtil.NullOrEmpty(userId_)){
    			userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            logger.info("提交工单评价开始userId："+userId);
            Map<String, Object> result = maintenanceorderService.checkEvaluation(type,orderNumber,userId);
            logger.info("校验是否存在可评价工单");
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("Exception:", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        } finally {
            logger.info("MaintenanceordereController.checkEvaluation==>end...");
        }
    }
}
